﻿using System.Data;
using System.Windows.Forms;
using ApplicationLibrary.CounterManager;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using Vintner.Items.ModuleData;
using Vintner.Masters.Forms;
using Vintner.Masters.ModuleData;

namespace Vintner.Masters.Library
{
    internal class CustomersCreator : Customers, IDocumentDataManager
    {
        public CustomersCreator()
        {
        }

        public bool Find(string code)
        {
            RadarCustomerParam param = new RadarCustomerParam(code);
            return FindRecord(param);
        }

        public void SetData1(string code, string ragsoc)
        {
            CounterControl fncCounter = Controls["fncCustomer"] as CounterControl;
            fncCounter.Text = code;
            fncCounter.IsAutomatic = false;
            Controls["txtCompany"].Text = ragsoc;
        }

        public void SetData2(string ind, string cap, string city, string county)
        {
            Controls["tabControl1"].Controls["tpgAddress"].Controls["txtAddress"].Text = ind;
            Controls["tabControl1"].Controls["tpgAddress"].Controls["mtbZip"].Text = cap;
            Controls["tabControl1"].Controls["tpgAddress"].Controls["txtCity"].Text = city;
            Controls["tabControl1"].Controls["tpgAddress"].Controls["txtCounty"].Text = county;
        }

        public void SetData3(string tel1, string rif1, string tel2, string rif2, string fax)
        {
            if (!string.IsNullOrEmpty(tel1))
                Controls["tabControl1"].Controls["tpgAddress"].Controls["txtPhone1"].Text = tel1;
            if (!string.IsNullOrEmpty(rif1))
                Controls["tabControl1"].Controls["tpgAddress"].Controls["txtRefer1"].Text = rif1;
            if (!string.IsNullOrEmpty(tel2))
                Controls["tabControl1"].Controls["tpgAddress"].Controls["txtPhone2"].Text = tel2;
            if (!string.IsNullOrEmpty(rif2))
                Controls["tabControl1"].Controls["tpgAddress"].Controls["txtRefer2"].Text = rif2;
            if (!string.IsNullOrEmpty(fax))
                Controls["tabControl1"].Controls["tpgAddress"].Controls["txtFax"].Text = fax;
        }

        public void SetData4(string pIva, string cFis, string Dest)
        {
            if (!string.IsNullOrEmpty(pIva))
                Controls["tabControl1"].Controls["tpgFiscalData"].Controls["mtbTaxNo"].Text = pIva;

            if (!string.IsNullOrEmpty(cFis))
                Controls["tabControl1"].Controls["tpgFiscalData"].Controls["mtbFiscalCode"].Text = cFis;

            if (!string.IsNullOrEmpty(Dest))
                Controls["tabControl1"].Controls["tpgAddress"].Controls["txtDestGoods1"].Text = Dest;
        }

        public void AddPrice(string item, string cat)
        {
            ExtendedDataGridView dgw = (ExtendedDataGridView)Controls["tabControl1"].Controls["tpgItemCustomer"].Controls["TabItemCustomer"].Controls["dgwItemCustomer"];
            foreach (DataGridViewRow dr in dgw.Rows)
            {
                if (DataGridViewRowValues.GetValue<string>(dr, VI_ItemCustomerPrice.Category) == cat &&
                    DataGridViewRowValues.GetValue<string>(dr, VI_ItemCustomerPrice.Item) == item)
                    return;
            }

            DataRowView drw = dgw.AddNewRow();
            DataRowViewValues.SetValue<string>(drw, VI_ItemCustomerPrice.Item, item);
            DataRowViewValues.SetValue<string>(drw, VI_ItemCustomerPrice.Category, cat);
        }
    }
}